package com.tool.common.config;

import org.apache.ibatis.session.SqlSession;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author: zhoubin
 * @date: 2022/9/15
 */
public class MapperInterceptor<T> implements InvocationHandler {

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        try ( SqlSession session = DatabaseConfig.getSqlSessionFactory().openSession(true)){

            System.out.println("代理方法为: " + method);

            Class<?> c = method.getDeclaringClass();
            T dao = (T) session.getMapper(c);
            return method.invoke(dao, objects);
        }
    }
}
